package com.manor.dynamic.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.manor.common.Constants;
import com.manor.common.glide.ImgLoader;
import com.manor.common.utils.ProcessImageUtil;
import com.manor.common.utils.ToastUtil;
import com.manor.common.utils.WordUtil;
import com.manor.dynamic.R;
import com.manor.dynamic.upload.UploadBean;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by cxf on 2018/6/20.
 * 聊天时候选择图片的Adapter
 */

public class ChooseImageAdapter extends RecyclerView.Adapter<ChooseImageAdapter.Vh> {

    private Context mContext;
    private static final int POSITION_NONE = -1;
    private List<UploadBean> mList;
    private LayoutInflater mInflater;
    private int mSelectedPosition;
    private Drawable mCheckedDrawable;
    private Drawable mUnCheckedDrawable;
    private View.OnClickListener mOnClickListener;
    private SparseArray<UploadBean> mSelectList;
    private int mSelectNum;
    private boolean isNeedCrop = false;
    private ProcessImageUtil mImageUtil=null;

    public ChooseImageAdapter(Context context, List<UploadBean> list,boolean boo,ProcessImageUtil mImgUtil) {
        this.mImageUtil = mImgUtil;
        Logger.e("mProcessImageUtil boo +==="+ (mImageUtil!=null));
        this.isNeedCrop = boo;
        initData(context, list, 1);
    }

    public ChooseImageAdapter(Context context, List<UploadBean> list,int selectedNum) {

        initData(context, list, selectedNum);
    }

    private void initData(Context context, List<UploadBean> list,int selectedNum){
        mContext = context;
        mList = list;
        mSelectList=new SparseArray<>();
        mSelectNum=selectedNum;
        mInflater = LayoutInflater.from(context);
        mSelectedPosition = POSITION_NONE;
        mCheckedDrawable = ContextCompat.getDrawable(context, R.mipmap.icon_checked);
        mUnCheckedDrawable = ContextCompat.getDrawable(context, R.mipmap.icon_checked_none);
        mOnClickListener = itemClickListener;
    }

    private View.OnClickListener itemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Object tag = v.getTag();
            if (tag == null) {
                return;
            }
            int position = (int) tag;
            if (isNeedCrop){
                //cropImage(mList.get(position).getOriginFile());
                Logger.e("mImageUtil == "+(mImageUtil!=null));
                if (mImageUtil!=null) {
                    mImageUtil.cropImage(mList.get(position).getOriginFile(),true);
                }
//                EventBus.getDefault().post(mList.get(position).getOriginFile());
            }else {
                setItemCheck(position);
            }

        }
    };


    private void setItemCheck(int position){
        if (position == mSelectedPosition) {
            mSelectList.remove(position);
            mList.get(mSelectedPosition).setChecked(false);
            notifyItemChanged(mSelectedPosition, Constants.PAYLOAD);
            mSelectedPosition=POSITION_NONE;
            return;
        }
        if (mSelectList.size()+mSelectNum<Constants.DYNAMIC_IMG_MAX_NUM) {
            mSelectList.put(position,mList.get(position));
            mList.get(position).setChecked(true);
            notifyItemChanged(position, Constants.PAYLOAD);
        }else {
            ToastUtil.show(WordUtil.getString(R.string.img_select_tip));
        }
        mSelectedPosition = position;
    }

    @Override
    public Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        if (isNeedCrop) {
            return new Vh(mInflater.inflate(R.layout.item_chat_choose_img, parent, false),true);
        }else {
            return new Vh(mInflater.inflate(R.layout.item_chat_choose_img, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(Vh vh, int position) {

    }

    @Override
    public void onBindViewHolder(Vh vh, int position, List<Object> payloads) {
        Object payload = payloads.size() > 0 ? payloads.get(0) : null;
        vh.setData(mList.get(position), position, payload);
    }



    public List<UploadBean> getSelectedFile() {
        List<UploadBean> uploadBeanList=new ArrayList<>();
        for (int i = 0; i <mSelectList.size() ; i++) {
            UploadBean bean=mSelectList.valueAt(i);
            bean.setType(UploadBean.IMG);
            uploadBeanList.add(bean);
        }
        return uploadBeanList;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class Vh extends RecyclerView.ViewHolder {

        ImageView mCover;
        ImageView mImg;

        public Vh(View itemView) {
            super(itemView);
            init(itemView,false);
        }

        public Vh(View itemView ,boolean isNeedHide){
            super(itemView);
            init(itemView,isNeedHide);
        }

        private void init(View itemView,boolean isNeedHide){
            mCover = (ImageView) itemView.findViewById(R.id.cover);
            mImg = (ImageView) itemView.findViewById(R.id.img);
            itemView.setOnClickListener(mOnClickListener);
            if (isNeedHide)
            mImg.setVisibility(View.GONE);
        }

        void setData(UploadBean bean, int position, Object payload) {
            itemView.setTag(position);
            if (payload == null) {
                ImgLoader.display(mContext, bean.getOriginFile(), mCover);
            }
            mImg.setImageDrawable(bean.isChecked() ? mCheckedDrawable : mUnCheckedDrawable);
        }
    }

}
